package com.bingxue.edu.management.auth.service;

import cn.hutool.json.JSONUtil;
import com.bingxue.edu.common.enums.SocialAccountEnum;
import com.bingxue.edu.common.enums.UserType;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.system.mapper.SocialAccountMapper;
import com.bingxue.edu.management.system.model.entity.SocialAccount;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.bingxue.edu.management.system.model.entity.table.SocialAccountTableDef.SOCIAL_ACCOUNT;

/**
* 社交账户业务实现类
*
* @author panbo
* @since 2024/07/24 18:12
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class SocialAccountService extends BaseService<SocialAccountMapper, SocialAccount> {

    /**
     * 根据来源和开放 ID 查询
     *
     * @param source 来源
     * @param openId 开放 ID
     * @return 用户社会化关联信息
     */
    public SocialAccount getBySourceAndOpenId(UserType userType, String source, String openId) {
        return null;
    }

    /**
     * 绑定第三方账号
     * 该账号已绑定该平台无法再绑定
     *
     * @param authUser
     * @param currentUserId
     */
    public void bind(AuthUser authUser, String currentUserId) {
        //获取第三方登录信息
        String source = authUser.getSource();
        String openId = authUser.getUuid();

        //判断是否已经绑定该平台
        List<SocialAccount> userSocialList = this.listByUserId(currentUserId);
        Set<String> boundSocialSet = userSocialList.stream().map(SocialAccount::getSource).collect(Collectors.toSet());
        String label = SocialAccountEnum.valueOf(source).getLabel();
        ValidationUtils.throwIf(boundSocialSet.contains(source), "您已经绑定过了 [{}] 平台，请先解绑", label);
        //判断是否被其他用户绑定
        SocialAccount socialAccount = this.getBySourceAndOpenId(UserType.EMPLOYEE,source, openId);
        ValidationUtils.throwIfNotNull(socialAccount, "[{}] 平台账号 [{}] 已被其他用户绑定", label, authUser.getUsername());
        //插入数据库
        socialAccount = new SocialAccount();
        socialAccount.setUserId(currentUserId);
        socialAccount.setSource(source);
        socialAccount.setOpenId(openId);
        socialAccount.setMetadata(JSONUtil.toJsonStr(authUser));
        socialAccount.setLastLoginTime(LocalDateTime.now());
        this.save(socialAccount);
    }

    /**
     * 根据userId获取第三方账号列表
     * @param currentUserId 用户id
     * @return
     */
    public List<SocialAccount> listByUserId(String currentUserId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SOCIAL_ACCOUNT.ALL_COLUMNS)
                .where(SOCIAL_ACCOUNT.USER_ID.eq(currentUserId));
        return this.list(queryWrapper);
    }

    public void deleteBySourceAndUserId(String source, String currentUserId) {
        this.updateChain().where(SOCIAL_ACCOUNT.SOURCE.eq(source)).and(SOCIAL_ACCOUNT.USER_ID.eq(currentUserId)).remove();
    }
}
